package com.xj.controller;

import com.xj.model.User;
import com.xj.service.LoginService;
import com.xj.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletResponse;

/**
 * 纯页面跳转，不需要try。有具体操作的请求，可以设置共同的异常页面。
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 注册页面
     * @return
     */
    @RequestMapping(path = {"/login/register"}, method = {RequestMethod.GET})
    public String register() {
        return "login/register";
    }

    /**
     * 注册页面的后台具体操作
     * @param model
     * @param response
     * @param name
     * @param email
     * @param password
     * @return
     */
    @RequestMapping(path = {"/login/register/do"}, method = {RequestMethod.POST})
    public String doRegister(Model model, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("password") String password) {
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setPassword(password);

        /**
         * 注册用户，写入Cookie
         */
        try {
            String t = loginService.register(user);
            CookieUtils.writeCookie("t", t, response);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }

    /**
     * 登陆页面
     * @return
     */
    @RequestMapping(path = {"/login/login"}, method = {RequestMethod.GET})
    public String login() {
        return "login/login";
    }

    /**
     * 登陆页面的后台操作
     * @param model
     * @param response
     * @param email
     * @param password
     * @return
     */
    @RequestMapping(path = {"/login/login/do"}, method = {RequestMethod.POST})
    public String doLogin(Model model, HttpServletResponse response, @RequestParam("email") String email, @RequestParam("password") String password) {
        //写入最新的cookie
        try {
            String t = loginService.login(email, password);
            CookieUtils.writeCookie("t", t, response);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }

    /**
     * 注销的后台操作，从cookie中获取值
     * @param model
     * @param t
     * @return
     */
    @RequestMapping(path = {"/login/logout/do"}, method = {RequestMethod.GET})
    public String doLogout(Model model, @CookieValue("t") String t) {
        try {
            loginService.logout(t);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }
}
